(function() {
    "use strict";

    angular.module("gaCommonCtrl", ['angularUtils.directives.dirPagination'])
        .controller('PaginationBaseController', PaginationBaseController);

    function PaginationBaseController(wtPagination, vm,  load) {
        vm.currentPage = wtPagination.initCurrentPage('page');
        vm.pageSize = vm.pageSize || wtPagination.getDefaultPageSize();
        wtPagination.setPageSize(vm.pageSize);
        vm.query = {
            offset: wtPagination.getOffset(vm.currentPage),
            limit: vm.pageSize
        };

        vm.onPageChanged = function(page) {
            setPageInfo(page);
            load();
        };

        vm.resetPageInfo = function() {
            if (vm.currentPage !== 1) {
                setPageInfo(1);
            } else {
                load();
            }
        };

        function setPageInfo(page) {
            wtPagination.setCurrentPage(page);
            vm.currentPage = page;
            vm.query.offset = wtPagination.getOffset(page);
        }
    }
})();